import request from "@/utils/request";
import router from "@/router";
export default {
    namespaced: true,
    state: {
        addressArr: []
    },
    actions: {

    },
    mutations: {
        // 新增地址
        addAddress(state, form) {
            request.post('/address/addressAdd', form).then(res => {
                if (res.code == 20000) {
                    this.commit('addressStore/getAddressList')
                }
            })
        },
        // 编辑地址
        editAddress(state, form) {
            request.put('/address/addressEdit', form).then(res => {
                if (res.code == 20000) {
                    this.commit('addressStore/getAddressList')
                }
            })
        },
        // 获取地址列表
        getAddressList(state) {
            request.get('/address/getAddressList').then(res => {
                if (res.code == 20000) {
                    state.addressArr = res.data
                }
            })
        },
        // 删除地址
        deleteAddress(state, id) {
            request.delete('/address/addressDel?addressId=' + id).then(res => {
                if (res.code == 20000) {
                    this.commit('addressStore/getAddressList')
                }
            })
            state.addressArr = state.addressArr.filter(item => item.id != id)
        },
        // 设置默认地址
        setDefault(state, address) {
            // this.commit('addressStore/editAddress', address)
            state.addressArr.forEach(item => {
                if (item.isDefault) {
                    item.isDefault = 0
                }
            })
            const findResult = state.addressArr.find(item => item.id === address.id)
            if (findResult) {
                findResult.isDefault = 1
                this.commit('addressStore/editAddress', findResult)

            }
        }
    },
    getters: {

    }
}